package com.springboot.controller;

import com.springboot.constant.HealthRecordExecl;
import com.springboot.constant.WxUserExecl;
import com.springboot.dto.HealthRecordDTO;
import com.springboot.enums.ErrorEnum;
import com.springboot.exception.MyException;
import com.springboot.service.HealthRecordService;
import com.springboot.utils.Response;
import com.springboot.vo.PageVO;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;

/**
 * @author: lzh
 */
@RestController
public class HealthRecordController {

    @Autowired
    HealthRecordService healthRecordService;

    /**
     * @descrition:  扫码后记录用户
     * @param healthRecordDTO :
     * @return : com.springboot.utils.Response
     */
    @PostMapping("/wx/health_record")
    @PreAuthorize("hasRole('PROSECUTOR')")
    public Response addHealthRecord(@Valid HealthRecordDTO healthRecordDTO) {
        healthRecordService.addHealthRecord(healthRecordDTO);
        return Response.success();
    }

    /**
     * @descrition:  获取扫码记录列表
     * @param pageNum :
     * @param pageSize :
     * @param keywords :
     * @param time :
     * @return : com.springboot.utils.Response
     */
    @GetMapping("/wx/health_record")
    @PreAuthorize("hasRole('PROSECUTOR')")
    public Response getHealthRecordList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                        @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                        @RequestParam(required = false) String keywords,
                                        @RequestParam(required = false) Date time) {
        PageVO pageVO = healthRecordService.getHealthRecordList(pageNum, pageSize, keywords, time);
        return Response.success(pageVO);
    }

    /**
     * @descrition:  后台获取扫码记录列表
     * @param pageNum :
     * @param pageSize :
     * @param keywords :
     * @param begin :
     * @param end :
     * @return : com.springboot.utils.Response
     */
    @GetMapping("/admin/health_record")
    public Response adminGetHealthRecordList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                             @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                             @RequestParam(required = false) String keywords,
                                             @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date begin,
                                             @RequestParam(required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date end) {
        PageVO pageVO = healthRecordService.adminGetHealthRecordList(pageNum, pageSize, keywords, begin, end);
        return Response.success(pageVO);
    }

    /**
     * @descrition:  获取微信用户记录execl表格
     * @param response :
     * @return : void
     */
    @GetMapping("/admin/health_record/execl")
    public void dowloadExecl(HttpServletResponse response) {
        HSSFWorkbook wb = healthRecordService.dowloadExecl();
        try {
            //设置Http响应头告诉浏览器下载这个附件
            response.setHeader("Content-Disposition"
                    , "attachment;filename=" +  URLEncoder.encode(HealthRecordExecl.SHEETNAME, "utf-8") + ".xls");
            OutputStream outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MyException(ErrorEnum.DOWLOAD_EXECL_ERROR);
        }
    }
}
